package com.cookie; /**
 * @author wzm
 * @create 2023-06-29 11:52
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookie/add")
public class ServletD extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*补充  对于在多层级Servlet存储的cookie
         * 如： cookieA存储在 路径      http://....:8080/a/c
         * 此时 http://.......8080/路径 没有带a 无法获取cookie
         *
         * cookie存在默认有效路径，只让当前存储路径下的资源可以访问
         * 有效路径：
         * cookie有个属性path,他可以过滤哪些cookie可以发送给服务器，哪些不发
         *
         */
        Cookie cookie1 = new Cookie("name1","李四");
       //通常建议将cookie的有效路径设置项目路径
        cookie1.setPath(request.getContextPath());


        response.addCookie(cookie1);

    }
}
